<?php

class BugsController extends Controller
{
	public function actionIndex()
	{  
		 $time = date('Y-m-d h:m:s');
	
		
		$pid=Yii::app()->request->getParam('pid') ;
		$uid=Yii::app()->request->getParam('uid');
	    $blid=Yii::app()->request->getParam('blid');
		if ($pid==NULL) throw new CHttpException('404','Project not found');
		$NewBugList= new BugList();
	    $Bugs=array();
	    $AddBug= new Bug();
		// GET ALL BUGS IN ALL BUG LISTS IN $pid PROJECT
		if($blid == NULL){
			$BugList= BugList::model()->findAll('bug_project_id',$pid);
			foreach ($BugList as $item){
	            $Bug['model']= $item;
	            if($uid !=NULL)
	            	$Bug['AllBug']=Bug::model()->findAllByAttributes(array('bug_list_id'=>$item->bug_list_id,'bug_posted_by_user_id'=>$uid));
	            else 
	            	$Bug['AllBug']=$item->bugs;		
	            
				$Bugs[]=$Bug;
			}
		}
		else {
			$BugList=BugList::model()->findByPk($blid);
			$Bug['model']=$BugList;
			$Bug['AllBug']=$BugList->bugs;
			$Bugs[]=$Bug;
		}
		
		
		//GET ALL USER IN PROJECT
		 $modelProject = Project::model()->findByPk($pid);
         $users = $modelProject->projectUsers;
		// add NEWBUGLIST
	   if(isset($_POST['BugList'])){
	   	$NewBugList->attributes= $_POST['BugList'];
	   	$NewBugList->bug_project_id= $pid;
	   	if($this->BugListNameCheck($NewBugList->bug_list_name, $NewBugList->bug_project_id)==0){
	   	if($NewBugList->save()){
	   	  Yii::app()->user->setFlash('index','Add new BugList Complete');
	   		$this->refresh();
	   	}
	   	}
	   }
	   else Yii::app()->user->setFlash('index','BugList is exist');
	   //Find All Bug priority
	    //$BugPriority= Bug::model()->findAllByAttributes('bug_priority');
		//Get All BUgList to show
		$AllBugList=BugList::model()->findAll('bug_project_id',$pid);	 
		$this->render('index',array('Bugs'=> $Bugs,'NewBugList'=>$NewBugList,'pid'=>$pid,
		'users'=>$users,'AllBugList'=>$AllBugList,'AddBug'=>$AddBug));
	}

	 public function actionAdd(){
	 	 	$pid=Yii::app()->request->getParam('pid');
	 	 	if($pid==NULL) throw new CHttpException('404','Not found');
	 	 	if(isset($_POST['Bug'])){
	 	 		$Bug = new Bug();
	 	 		$Bug->attributes = $_POST['Bug'];
	 	 		$Bug->bug_happened_date = Html::formatDateTime($Bug->bug_happened_date, "MM/dd/yyyy", "Y-m-d"); 
	 	 		$Bug->bug_posted_by_user_id = Yii::app()->user->id;
	 	 		if($Bug->save()){
	 	 			Yii::app()->user->setFlash('success','Add new bug successful');
	 	 		}
	 	 		else 
	 	 			Yii::app()->user->setFlash('error','Error! Add new bug fail !');
	 	 		$this->redirect(array('bugs/index','pid'=>$pid));
	 	 	}
	
	    	
		   
		
			
	 }
 /**
  * Detail action 
  */
 public function actionBugDetail(){
 	$bid=Yii::app()->request->getParam('bid');
 	$pid=Yii::app()->request->getParam('pid');
 	
 	if($bid==NULL) throw new CHttpException('404','Bug\'s not found ');
    $Bug = Bug::model()->findByPk($bid);
    Yii::log('Chung : '.$Bug->bug_id);
    	$NewBugList= new BugList();
     $CreatedUser = User::model()->findByPk($Bug->bug_posted_by_user_id);
    // add NEWBUGLIST
	   if(isset($_POST['BugList'])){
	   	$NewBugList->attributes= $_POST['BugList'];
	   	$NewBugList->bug_project_id= $pid;
	   	if($this->BugListNameCheck($NewBugList->bug_list_name, $NewBugList->bug_project_id)==0){
	   	if($NewBugList->save()){
	   	  Yii::app()->user->setFlash('index','Add new BugList Complete');
	   		$this->redirect($this->createUrl('/bugs/index',array('pid'=>$pid)));
	   	}
	   	}
	   }
	   else Yii::app()->user->setFlash('index','BugList is exist');
	   //get All BUg List IN project
	   $AllBugList=$this->getBugListName($pid);
 	$this->render('bugDetail',array('Bug'=> $Bug,'NewBugList'=>$NewBugList,
 	'AllBugList'=>$AllBugList,'pid'=>$pid,'CreatedUser'=>$CreatedUser,));
 }
 /**
  * Delete Bug List action
  */
 public function actionDeleteBugList(){
  $blid=Yii::app()->request->getPost('blid');
  $pid=Yii::app()->request->getPost('pid');
  if($blid==NULL) throw new CHttpException('404','Bug\'s not found');
  $BugList = BugList::model()->findByPk($blid);
  
 if ( $BugList->delete())
 	echo 1;
 
  //$this->redirect($this->createUrl('/bugs/index',array('pid'=>$pid)));
 }
 /**
  * Delete bug in List action
  */
  public function actionDeleteBug(){
   $bid=Yii::app()->request->getParam('bid');
   $pid=Yii::app()->request->getParam('pid');
   if($bid==NULL) throw new CHttpException('404','Bug\'s not found');
   $Bug= Bug::model()->findByPk($bid);
   if($Bug->delete()){
   	$this->redirect($this->createUrl('/bugs/index',array('pid'=>$pid,)));
   }
  }
 /**
  * Edit Bug List action
  */
 public function actionEditBugList(){
 	//$this->layout="ajax/main.php";
 	$BlName =Yii::app()->request->getPost('blName');
 	$BlDescription = Yii::app()->request->getPost('blDescript');
 	$BugListId = Yii::app()->request->getPost('bugListId');
 	$BugList = BugList::model()->findByPk($BugListId);
 	//$check = $BugList->updatebyPk(''array('bug_list_name'=>$BlName,'bug_list_description'=>$BlDescription));
 	$BugList->bug_list_name=$BlName;
 	$BugList->bug_list_description=$BlDescription;
 	if($BugList->save()){
 		echo 1;
 	}
 	//Yii::log('Chung:'.$check);
 	//echo $check; 
 	 
 }
 /**
  * Edit Bug action
  */
 public function actionEditBug(){
 	$bid=Yii::app()->request->getParam('bid');
 	$pid=Yii::app()->request->getParam('pid');
 	$Bug= Bug::model()->findByPk($bid);
 	if(isset($_POST['Bug'])){
 	$Bug->attributes= $_POST['Bug'];
 	if($Bug->save()){
 	 $this->redirect($this->createUrl('/bugs/bugDetail',array('bid'=>$bid,'pid'=>$pid)));
 	}
 	}
 }
 /**
	 * Check bug_list_name in project/ if it exist return 1.
	 */
 public function BugListNameCheck($bugListName,$projectID){
 	$check = BugList::model()->find(array('select'=>'bug_list_name','condition' =>'
 	bug_list_name=:bugName && bug_project_id=:projectID',
 	'params'=>array(':bugName'=>$bugListName,':projectID'=>$projectID),));
 	if($check == NULL) return 0;
 	else return 1;
 }
 public function BugNameCheck($bugName,$bugListID){
 	$check=Bug::model()->find(array('select'=>'bug_name','condition'=>'
 	bug_name=:bugName && bug_list_id=:bugListID',
 	'params'=> array(':bugName'=>$bugName,':bugListID'=>$bugListID),));
 	if($check == NULL)return 0;
 	else return 1;
 }
 public function getBugListName($pid){
 	$BugList = BugList::model()->findAllByAttributes(array('bug_project_id'=>$pid));
 	//$model=CHtml::listData($BugList,'bug_project_id','bug_list_name');
 	return $BugList;
 }
 
}